package com.yidu.logisticssystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.logisticssystem.entity.LogisticsGate;
import com.yidu.logisticssystem.entity.LogisticsMap;
import com.yidu.logisticssystem.entity.LogisticsPost;
import com.yidu.logisticssystem.entity.LogisticsWageNorm;
import com.yidu.logisticssystem.entity.vo.WageNormVo;
import com.yidu.logisticssystem.service.LogisticsMapService;
import com.yidu.logisticssystem.service.LogisticsPostService;
import com.yidu.logisticssystem.service.LogisticsWageNormService;
import com.yidu.logisticssystem.utils.BaseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 基本工资标准表(LogisticsWageNorm)表控制层
 *
 * @author 宋玉玲
 * @since 2021-03-17 14:59:21
 */
@Api(description = "基本工资标准表")
@RestController
@RequestMapping("logisticsWageNorm")
public class LogisticsWageNormController {
    /**
     * 服务对象
     */
    //jdk8这么写@Resource
    @Resource
    private LogisticsWageNormService logisticsWageNormService;



    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public LogisticsWageNorm selectOne(Integer id) {
        return this.logisticsWageNormService.queryById(id);
    }



    /**
     * 根据id删除
     * @param moneyId
     * @return
     */
    @ApiOperation("根据id删除基本工资标准数据")
    @PostMapping("delete")
    public BaseMessage delete(int moneyId){

        return logisticsWageNormService.deleteById(moneyId)>0?
                BaseMessage.succes():BaseMessage.fail();
    }

    /**
     * 根据id查询
     * @param moneyId
     * @return
     */
    @ApiOperation("根据id查询基本工资标准数据")
    @PostMapping("selectId")
    public BaseMessage selectId(int moneyId){
        LogisticsWageNorm logisticsWageNorm=logisticsWageNormService.queryById(moneyId);
        return BaseMessage.succes(logisticsWageNorm);
    }

    /**
     * 根据id判断添加或修改
     * @param logisticsWageNorm
     * @return
     */
    @ApiOperation("添加和基本工资标准表，id为空为添加，id有值为修改")
    @PostMapping("addOrUpdate")
    public BaseMessage addOrUpdate(Integer moneyId, LogisticsWageNorm logisticsWageNorm) {
        BaseMessage bm = new BaseMessage();

        //根据条件查询行数，query
        int rows=logisticsWageNormService.query(logisticsWageNorm);
        //判断行数为零可以添加修改，不为零不可添加修改
        if (rows==0) {
            //判断id为零就添加，不为零就修改
            if (moneyId == null) {
                //为零添加，insertSelective
                logisticsWageNormService.insertSelective(logisticsWageNorm);
                bm.setState(1);
                bm.setMessage("增加成功");
            } else {
               //不为零修改 update
                logisticsWageNormService.update(logisticsWageNorm);
                bm.setState(0);
                bm.setMessage("修改成功");
            }
        }else {
            bm.setState(2);
            bm.setMessage("标准已存在");
        }
        return bm;
    }

    /**
     * 批量删除
     * @param moneyId
     * @return
     */
    @ApiOperation("批量删除")
    @PostMapping("batch")
    public BaseMessage batch(String moneyId){
        return logisticsWageNormService.delete(moneyId) > 0 ?
                BaseMessage.succes() : BaseMessage.fail();

    }




    /**
     * 查询所有
     * @param vo
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("查询基本工资标准所有")
    @GetMapping("select")
    public Map<String, Object> queryAll(WageNormVo vo, int page, int limit){
        //分页助手
        PageHelper.startPage(page,limit);
        //map
        Map<String, Object> map=new HashMap<>();
        //页面上查询所有
        List<LogisticsWageNorm> list=logisticsWageNormService.queryAll(vo);
        //把查询的所有放入pageInfo中分页
        PageInfo<LogisticsWageNorm> pageInfo=new PageInfo<>(list);
        map.put("code",0);
        map.put("msg","");
        map.put("data",pageInfo.getList());
        map.put("count",pageInfo.getTotal());
        return map;
    }





}
